package com.bjpowernode.p2p.service.user.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.bjpowernode.p2p.constant.Redis;
import com.bjpowernode.p2p.constant.Report;
import com.bjpowernode.p2p.dao.ReportDao;
import com.bjpowernode.p2p.service.user.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
@Service(interfaceClass = UserService.class, version = "1.0")
public class UserServiceImpl implements UserService {
    @Autowired
    ReportDao reportDao;
    @Autowired
    RedisTemplate<Object, Object> redisTemplate;

    @Override
    public Integer countUser() {
        Integer countUserReport = (Integer) redisTemplate.opsForValue().get(Report.COUNT_USER);
        if (!ObjectUtils.allNotNull(countUserReport)) {
            synchronized (this) {
                countUserReport = (Integer) redisTemplate.opsForValue().get(Report.COUNT_USER);
                if (!ObjectUtils.allNotNull(countUserReport)) {
                    countUserReport = reportDao.countUserReport();
                    log.info("数据库查询countUserReport.....................");
                    redisTemplate.opsForValue().set(Report.COUNT_USER, countUserReport, Redis.TIMEOUT, TimeUnit.SECONDS);
                }
            }
        }


        return countUserReport;
    }
}
